﻿var oLastTr ;


//单击行时的处理
function doOnTrClick(oTr)
{
   
	//查看模式则不处理
	if (__mode.value == "3")
	{
		return;
	}	
	//如果是通过click方法触发事件来模拟单击或者单击的是内嵌table中的元素，则oTr为event对象,故不存在tagName的属性
	if (!oTr.tagName)
	{
		oTr = event.srcElement;			//Td或内嵌table中的元素
		
		//循环获得Tr元素
		while(oTr.tagName.toUpperCase() != "TR" || (!oTr.oid))
		{
			oTr = oTr.parentElement;
		}
	}
		
	//点击的同一行则不处理
	if (oLastTr == oTr)
	{
		return;
	}
	
	//改变行的编辑状态
	setTrReadOnly(oLastTr);
	setTrEdit(oTr);

}
//使指定行可编辑
function setTrEdit(oTr)
{

	if(!oTr)
	{
		return;
	}
	oTr.bgColor = "#CAD3E4";
	
	//已是编辑状态则不处理
	if(oTr.EditState == "1" || !oTr.EditState)
	{
		return;
	}
	var sRooms;			    //计划销售套数 3
	var sBldArea;	    	//计划销售面积 4
	var sAmount;			//计划销售金额 5
	var sPrice;     		//计划销售均价 6
	var sJHHKAmount;		//计划回款金额 7
	var sChecked = ""
	
	//计划销售套数
	sRooms = oTr.cells["Rooms"].innerText.replace(/,/g,"");
	oTr.cells["Rooms"].innerText = "";
	oTr.cells["Rooms"].insertAdjacentHTML("BeforeEnd", '<INPUT type="text" value="' + parseInt(sRooms) + '" name="Rooms" class="num" min="0" max="9999999" maxlength=8 onbeforedeactivate="updateDate(this)" style="width:98%">');	
	
	//计划销售面积	
	sBldArea = oTr.cells["BldArea"].innerText.replace(/,/g,"");
	oTr.cells["BldArea"].innerText = "";
	oTr.cells["BldArea"].insertAdjacentHTML("BeforeEnd", '<INPUT type="text" value="' + parseFloat(sBldArea) + '" name="BldArea"　 class="num" acc="2" dt="float" min="0" max="999999999" maxlength=11 onbeforedeactivate="updateDate(this)" style="width:98%">');	
	
	
	//计划销售金额	
	sAmount = oTr.cells["Amount"].innerText.replace(/,/g,"");
	oTr.cells["Amount"].innerText = "";
	oTr.cells["Amount"].insertAdjacentHTML("BeforeEnd", '<INPUT type="text" value="' + parseFloat(sAmount) + '" name="Amount" class="num" acc="2" dt="float" min="0" max="999999999999" maxlength=17  onbeforedeactivate="updateDate(this)" style="width:98%">');	
	
	
	//计划销售均价
	sPrice = oTr.cells["Price"].innerText.replace(/,/g,"");
	oTr.cells["Price"].innerText = "";
	oTr.cells["Price"].insertAdjacentHTML("BeforeEnd", '<INPUT type="text" value="' + parseFloat(sPrice) + '" name="Price" class="num" acc="2" dt="float" min="0" max="999999" maxlength=9 onbeforedeactivate="updateDate(this)" style="width:98%">');	
	
	//计划回款金额
	sJHHKAmount = oTr.cells["JHHKAmount"].innerText.replace(/,/g,"");
	oTr.cells["JHHKAmount"].innerText = "";
	oTr.cells["JHHKAmount"].insertAdjacentHTML("BeforeEnd", '<INPUT type="text" value="' + parseFloat(sJHHKAmount) + '" name="JHHKAmount" class="num" acc="2" dt="float" min="0" max="999999999999" maxlength=17 onbeforedeactivate="updateDate(this)" style="width:98%">');		
	
	oTr.EditState = "1";	//置为编辑状态	
	
	oLastTr = oTr;		//保存最后点击的行	
}
//使指定行不可编辑
function setTrReadOnly(oTr)
{

	if(!oTr)
	{
		return;
	}
	oTr.bgColor = "#FFFFFF";
	
	//已是非编辑状态则不处理
	if(oTr.EditState == "0" || !oTr.EditState)
	{
		return;
	}
	var sRooms;			    //计划销售套数 3
	var sBldArea;	    	//计划销售面积 4
	var sAmount;			//计划销售金额 5
	var sPrice;     		//计划销售均价 6
	var sJHHKAmount;		//计划回款金额 7
	var bChecked ;
	

	//计划销售套数
	sRooms = oTr.cells["Rooms"].firstChild.value;
    oTr.cells["Rooms"].innerHTML = "<NOBR>" + sRooms + "</NOBR>";
    oTr.cells["Rooms"].innerText = sRooms;		 
    			

	//计划销售面积。
	sBldArea = oTr.cells["BldArea"].firstChild.value; 
    oTr.cells["BldArea"].innerHTML = "<NOBR>"+ sBldArea +"</NOBR>";
	oTr.cells["BldArea"].innerText = sBldArea;	 
	 
  
	//计划销售金额 
	sAmount = oTr.cells["Amount"].firstChild.value; 
	oTr.cells["Amount"].innerHTML = "<NOBR>" + sAmount + "</NOBR>";	


	//计划销售均价
	sPrice = oTr.cells["Price"].firstChild.value;
	oTr.cells["Price"].innerHTML = "<NOBR>" + sPrice + "</NOBR>";
	oTr.cells["Price"].title = sPrice;
	
	//计划回款金额
	sJHHKAmount = oTr.cells["JHHKAmount"].firstChild.value;
	oTr.cells["JHHKAmount"].innerHTML = "<NOBR>" + sJHHKAmount + "</NOBR>";
	oTr.cells["JHHKAmount"].title = sJHHKAmount;
	
	oTr.EditState = "0";		//置为非编辑状态
	oLastTr = null; 	        //最后点击的行为空
}

//对象焦点失去前的校验，用于解决目前平台控件校验只能在onblur时造成的多控件争焦点的情况
function BeforeDeActivateForNum(obj) {
    //设置面积保留位
    //史海峰	2005-01-07
    if (obj.acc == "mj") {
        obj.acc = 4;
    }
    if (isNaN(obj.acc))			// 自定义小数位数
    {
        obj.acc = eval("FMT_" + obj.acc);
    }
    //
    var sValue = Trim(obj.value);
    var iMinValue = parseFloat(obj.min);
    var iMaxValue = parseFloat(obj.max);
    var bGrouping = (obj.grp == "true"); 		//是否带千分号

    var sFormat = ORG_NUMBER_FORMAT;
    var iAccuracy = parseInt(obj.acc, 10);

    var oEvent;

    if (sValue.length == 0) {
        event.returnValue = true;
        return true;
    }

    if (obj.dt == "integer") {
        sValue = FormatInteger(sValue, iMinValue, iMaxValue, bGrouping, sFormat);

        if (sValue) {
            event.returnValue = true;
            return true;
        }
        else {
            obj.select();
            event.returnValue = false;
            return false;
        }
    }
    else {
        sValue = FormatFloat(sValue, iMinValue, iMaxValue, bGrouping, sFormat, iAccuracy);

        if (sValue) {
            event.returnValue = true;
            return true;
        }
        else {
            obj.select();
            event.returnValue = false;
            return false;
        }
    }

    if (!obj.returnValue) {
        obj.select();
        event.returnValue = false;
        return false;
    }

    event.returnValue = true;
    return true;
}


function updateDate(objSelect)
{ 
  if (oLastTr)
	{
	    var sTable;
	    
	    sTable = document.all("tblData");
	   
	    var sLength = sTable.rows.length;
	    var iRooms = 0 ;
	    var iBldArea=0 ;
	    var iAmount=0  ;
	    var iPrice=0   ;
	    var iJHHKAmount=0 ;

	    if (parseFloat(objSelect.value.replace(/,/g, "")) == parseFloat(objSelect.returnValue.replace(/,/g, ""))) {	    
	        return
	    } 
	    
	    if (!BeforeDeActivateForNum(objSelect)) {
	        return false;	    
	    }
		  
		  
	    if (objSelect.value=="")
	    {
	        objSelect.setValue(0);
	    }
	    
	    if(sLength>1)
	    {
	        switch (objSelect.name)
	        {
	        //计划销售套数
	            case "Rooms":
	                for (i = 1; i < sLength; i++) {
	                    var temp = 0;
	                    if (sTable.rows[i].cells["Rooms"].innerText == "") {
	                        sTable.rows[i].cells["Rooms"].firstChild.value = objSelect.value.replace(/,/g, "");
	                        temp = parseInt(objSelect.value);
	                    }
	                    else {
	                        iRooms += parseInt(sTable.rows[i].cells["Rooms"].innerText.replace(/,/g, ""));
	                    }
	                    iRooms += temp;
	                }
	                parent.appForm.Rooms.value = iRooms;
	                parent.appForm.appForm_Rooms.setValue(AddFormatting(iRooms, 0, ORG_NUMBER_FORMAT, false, true));
	                break;
	            //计划销售面积。
	            case "BldArea":
	                var iRows = 0;
	                for (i = 1; i < sLength; i++) {
	                    var temp = 0;
	                    if (sTable.rows[i].cells["BldArea"].innerText == "") {
	                        sTable.rows[i].cells["BldArea"].firstChild.value = objSelect.value.replace(/,/g, "");
	                        temp = parseFloat(objSelect.value);
	                        iRows = i;
	                    }
	                    else {
	                        iBldArea += parseFloat(sTable.rows[i].cells["BldArea"].innerText.replace(/,/g, ""));
	                    }
	                    iBldArea += temp;

	                    temp = 0;
	                    if (iRows != i) {
	                        if (sTable.rows[i].cells["Amount"].innerText == "") {
	                            temp = parseFloat(sTable.rows[i].cells["Amount"].firstChild.value.replace(/,/g, ""));
	                        }
	                        else {
	                            iAmount += parseFloat(sTable.rows[i].cells["Amount"].innerText.replace(/,/g, ""));
	                        }
	                    }
	                    iAmount += temp;
	                }

	                //同一行表的销售金额改变
	                if (sTable.rows[iRows].cells["Price"].firstChild.value != "" && sTable.rows[iRows].cells["Price"].firstChild.value != 0) {
	                    var decAmount = parseFloat(parseFloat(sTable.rows[iRows].cells["BldArea"].firstChild.value.replace(/,/g, "")) * parseFloat(sTable.rows[iRows].cells["Price"].firstChild.value.replace(/,/g, "")));
	                    if (!(decAmount >= 0 && decAmount <= 999999999999)) {
	                        alert("当前行的计划销售金额必须处于0和999999999999之间");
	                        event.returnValue = false;
	                        return false;
	                    }

	                    sTable.rows[iRows].cells["Amount"].firstChild.value = AddFormatting(decAmount, 2, ORG_NUMBER_FORMAT, false, true);

	                    iAmount += decAmount;

	                    parent.appForm.Amount.value = iAmount;
	                    parent.appForm.appForm_Amount.setValue(AddFormatting(iAmount, 2, ORG_NUMBER_FORMAT, false, true));
	                }
	            
	                parent.appForm.Bldarea.value = iBldArea;
	                parent.appForm.appForm_Bldarea.setValue(AddFormatting(iBldArea, 2, ORG_NUMBER_FORMAT, false, true));

	                CalAmount();
	                break;	
	         //计划销售金额 
	         case "Amount":		                       
    	           for ( i=1;i<sLength;i++ )
    	           {
    	             var temp = 0;
    	             if(sTable.rows[i].cells["Amount"].innerText=="")
    	             {
    	                sTable.rows[i].cells["Amount"].firstChild.value= objSelect.value.replace(/,/g,"");
    	                temp  =  parseFloat(objSelect.value);	  
    	             }
    	             else
    	             {    	             
    	                iAmount += parseFloat(sTable.rows[i].cells["Amount"].innerText.replace(/,/g,""));	        	             
    	             }
    	              iAmount +=temp;	        
    	           }
    	           parent.appForm.Amount.value=iAmount;
    	           parent.appForm.appForm_Amount.setValue(AddFormatting(iAmount, 2, ORG_NUMBER_FORMAT, false, true));

    	           CalAmount(); 	
	               break;
	       　//计划销售均价	
	         case "Price": 
	                 var iRows;   
    	             for ( i=1;i<sLength;i++ )
    	            {
    	                 var temp = 0;
    	                 if(sTable.rows[i].cells["Price"].innerText=="")
    	                 {    	                     
    	                    iRows=i;
    	                } 
    	                   	               
    	                 if (iRows != i) {
    	                     if (sTable.rows[i].cells["Amount"].innerText == "") {    	                  
    	                         temp = parseFloat(parseFloat(sTable.rows[i].cells["Amount"].firstChild.value.replace(/,/g, "")));
    	                     }
    	                     else {
    	                         iAmount += parseFloat(sTable.rows[i].cells["Amount"].innerText.replace(/,/g, ""));
    	                     }
    	                 }
    	                 iAmount += temp;
    	            }	
    	           
    	           //同一行表的销售金额改变
    	           if (sTable.rows[iRows].cells["BldArea"].firstChild.value !="" && sTable.rows[iRows].cells["BldArea"].firstChild.value != 0)
    	           {
    	               var decAmount = parseFloat(parseFloat(sTable.rows[iRows].cells["BldArea"].firstChild.value.replace(/,/g, "")) * parseFloat(sTable.rows[iRows].cells["Price"].firstChild.value.replace(/,/g, "")));
    	               if (!(decAmount >= 0 && decAmount <= 999999999999)) {
    	                   alert("当前行的计划销售金额必须处于0和999999999999之间");
    	                   event.returnValue = false;
    	                   return false;
    	               }

    	               sTable.rows[iRows].cells["Amount"].firstChild.value = AddFormatting(decAmount, 2, ORG_NUMBER_FORMAT, false, true);

    	               iAmount += decAmount;
    	               
    	               parent.appForm.Amount.value = iAmount;
    	               parent.appForm.appForm_Amount.setValue(AddFormatting(iAmount, 2, ORG_NUMBER_FORMAT, false, true));

    	               CalAmount(); 
    	            }	  
	               break;	
	         	//计划回款金额
	         case "JHHKAmount":     
    	           for ( i=1;i<sLength;i++ )
    	           {
    	             var temp = 0;
    	             if(sTable.rows[i].cells["JHHKAmount"].innerText=="")
    	             {
    	                sTable.rows[i].cells["JHHKAmount"].firstChild.value= objSelect.value.replace(/,/g,""); 
    	                temp  =  parseFloat(objSelect.value);	    
    	                	              
    	             }    
    	             else
    	             {	            
    	                iJHHKAmount += parseFloat(sTable.rows[i].cells["JHHKAmount"].innerText.replace(/,/g,""));	        	             
    	             }
    	             iJHHKAmount+=temp;
    	           }
    	           parent.appForm.JHHKAmount.value=iJHHKAmount;
    	           parent.appForm.appForm_JHHKAmount.setValue(AddFormatting(iJHHKAmount, 2, ORG_NUMBER_FORMAT, false, true));
	               break;	       
	        }
	     }
	}
}


//监控销售金额，如有变化，则设置编辑区。
function CalAmount() {
    var decBldarea = parseFloat(parent.appForm.Bldarea.value);
    var decAmount = parseFloat(parent.appForm.Amount.value);

    if (decBldarea > 0) {
        var decPrice = Math.round(decAmount*100/decBldarea)/100;
     
        decPrice = AddFormatting(decPrice, 2, ORG_NUMBER_FORMAT, false, true);
        parent.appForm.Price.value = decPrice.replace(/,/g, "");
        parent.appForm.appForm_Price.setValue(decPrice);      
    }
    else {
        parent.appForm.Price.value = "0";
        parent.appForm.appForm_Price.setValue("0.00");
    }
}
			

//保存编辑项
function Save(setValue) 
{

    //使编辑行变为非编辑状态
         setTrReadOnly(oLastTr);
	    oLastTr = null;
	    
	    var i, j ,sUserXml;
        var sTable;
        try {
            sTable = appIframe.document.all("tblData");
        }
        catch (e) {
            alert("销售计划明细列表尚未加载完全！");
            event.returnValue = false;
            return;

        }
        
        //收集销售计划的详细数据
        if (sTable.rows.length <= 1 )
        {
            alert("当前系统中没有设置产品类型，不允许保存！");
            event.returnValue = false;
            return;
        }
	    
	    
	    var strReturn;
　　	
	    //判断如下：	    //如果所有一级产品类型对应的计划销售面积、计划销售金额、计划销售套数、计划回款金额、计划销售均价都为“0”时，系统提示：“请录入销售计划数据！”	
	    if (appForm.Rooms.value== 0 && appForm.Bldarea.value == 0 && appForm.Amount.value == 0 && appForm.Price.value == 0 && appForm.JHHKAmount.value == 0)
	    {
	        alert("请录入销售计划数据！")
	        return ;
	    }	 
	    else
	    {             
            (sTable.rows.length > 1 )
            {
                var delRowGuid="";
	            var xmlData = new ActiveXObject("Microsoft.XMLDOM");
	            xmlData.async = false;
	            xmlData.loadXML("<MyXml></MyXml>");

	            var iRooms = 0;
	            var decBldArea = 0.00;
	            var decAmount = 0.00;
	            var decPrice = 0.00;
	            var decJHHKAmount = 0.00;
	            
                var XSJHDetail= xmlData.createElement("XSJHDetail");	                
                             	            
                xmlData.documentElement.appendChild(XSJHDetail);
                for (var i = 1; i < sTable.rows.length; i++) {
                    if (sTable.rows[i].cells("Rooms").innerText == "") {
                        iRooms = Trim(sTable.rows[i].cells("Rooms").firstChild.value);
                        decBldArea = Trim(sTable.rows[i].cells("BldArea").firstChild.value);
                        decAmount = Trim(sTable.rows[i].cells("Amount").firstChild.value);
                        decPrice = Trim(sTable.rows[i].cells("Price").firstChild.value);
                        decJHHKAmount = Trim(sTable.rows[i].cells("JHHKAmount").firstChild.value);                       
                    }
                    else {
                       iRooms = Trim(sTable.rows[i].cells("Rooms").innerText);
                        decBldArea = Trim(sTable.rows[i].cells("BldArea").innerText);
                        decAmount = Trim(sTable.rows[i].cells("Amount").innerText);
                        decPrice = Trim(sTable.rows[i].cells("Price").innerText);
                        decJHHKAmount = Trim(sTable.rows[i].cells("JHHKAmount").innerText);
                    }

                    if (parseInt(iRooms.replace(/,/g, "")) == 0 && parseFloat(decBldArea.replace(/,/g, "")) == 0 && parseFloat(decAmount.replace(/,/g, "")) == 0 && parseFloat(decPrice.replace(/,/g, "")) == 0 && parseFloat(decJHHKAmount.replace(/,/g, "")) == 0) {
                        if (sTable.rows[i].SBDtlGUID != "") {
                            delRowGuid = delRowGuid + ";" + sTable.rows[i].SBDtlGUID;
                        }
                    }
                    else {
                        //插入 Xml 节点
                        var newElem = xmlData.createNode(1, "row", "");

                        //newElem.text = sTable.rows[i].WorkGUID;
                        newElem.setAttribute("Rooms", iRooms);
                        newElem.setAttribute("BldArea", decBldArea);
                        newElem.setAttribute("Amount", decAmount);
                        newElem.setAttribute("Price", decPrice);
                        newElem.setAttribute("JHHKAmount", decJHHKAmount);

                        if (sTable.rows[i].SBDtlGUID != "") {
                            newElem.setAttribute("SBDtlGUID", sTable.rows[i].SBDtlGUID);
                        }
                        newElem.setAttribute("BuildProductTypeGUID", sTable.rows[i].BuildProductTypeGUID);
                        XSJHDetail.appendChild(newElem);
                    }
                }
	            
	                if(delRowGuid!="")
	                {
	                    delRowGuid=delRowGuid.substring(1);
	                    XSJHDetail.setAttribute("delguids",delRowGuid);     
	                }

	                sUserXml = xmlData.xml;
	        }	
     
            appForm.userxml= sUserXml;     
            switch (setValue)
            {
               case '1':
               strReturn = appForm.Save();
               break;
              case '2':
               strReturn = appForm.SaveAndClose();
               break;
              case '3':
               strReturn = appForm.SaveAndNew();
               break;
            }	
		}     
}
